capture log close
log using "${path_log}/04_read_shiftshare.log", replace

*** Create data set with the latest sector information for each wave	
clear all 
	  
qui forval w = 1/41 {

	if `w' < 10 local w1 "0`w'"
	else 		local w1 "`w'"
	
	if inlist(`w',2,6,7,14,15,16,17,18,31,36) append using "${path_orig}\BOPF\bop_f_wave`w'_v2"  
	else if inlist(`w',1) 					  append using "${path_orig}\BOPF\bop_f_wave0`w'_v1" 
	else 			   			   	 	      append using "${path_orig}\BOPF\bop_f_wave`w'_v1" 
					
	preserve 
	 
	*** Handle missings
	recode firmsector (-6666 -9997 -9998 -9999 = .)
	
	*** Search latest wave with non-missing answer 
	bysort BBk_Sample_Key (wave): g counter = _n
	 
	gegen counter_firmsector = max(counter) if !mi(firmsector), by(BBk_Sample_Key)
	bysort BBk_Sample_Key: g aux_firmsector_l = firmsector if counter_firmsector == counter 
	gegen firmsector_l = max(aux_firmsector_l), by(BBk_Sample_Key)
	
	*** Keep only the latest wave
	keep if wave == `w'
			
	tempfile firmtype_`w'
	
	save `firmtype_`w''
	
	restore 
	 
} // y 

clear all

*** Append data for all waves besides pilot
qui forval w = 6/41 {

	append using `firmtype_`w''

} // y 

keep BBk_Sample_Key wave firmsector_l 
		
tempfile firmtype
	
save `firmtype'

clear all

*** Load data
qui forval w = 6/41 {
	
	if `w' < 10 local w1 "0`w'"
	else 		local w1 "`w'"
	
	if inlist(`w',2,6,7,14,15,16,17,18,31,36) append using "${path_orig}\BOPF\bop_f_wave`w'_v2" 
	else if inlist(`w',1) 					  append using "${path_orig}\BOPF\bop_f_wave0`w'_v1"
	else 			   			   	 	      append using "${path_orig}\BOPF\bop_f_wave`w'_v1" 
	
} // w

*** Merge with firm type
merge 1:1 BBk_Sample_Key wave using `firmtype'
keep if _merge == 3 
drop _merge

destring BBk_Sample_Key, replace

*** Handle missings
qui recode energy_prod energymix_? inflexpoint pricefuture pricepast (-6666 -9999 -9998 -9997 -9996 = .)

*** Using information on energy mix and intensity from pre-period
qui foreach v of varlist energymix_? energy_prod {
	 
	replace `v' = . if year >= 23
 
}

*** Add variables to all waves
foreach v of varlist energymix_? energy_prod {
		
	gegen aux_`v' = max(`v'), by(BBk_Sample_Key)
	replace `v' = aux_`v'
	drop aux_`v'

} // v
 
*** Generate fixed effects
gegen sector_wave = group(firmsector_l wave)

*** Account for outliers 
winsor2 inflexpoint pricefuture pricepast, cuts(2 98) trim replace 

*** Drop public sector firms  
drop if firmsector_l == 20   

xtset BBk_Sample_Key wave
gsort BBk_Sample_Key wave

g price_current = F3.pricepast // Shift price changes for last quarter
g price_realization = F12.pricepast // Price changes refer to the last 12 months 

*** Figure 3
qui reg price_realization pricefuture, cluster(BBk_Sample_Key)
local slope: di %04.3f _b[pricefuture]
local slope_se: di %04.3f _se[pricefuture]
local slope_p `= 2 * (1 - normal(_b[pricefuture] / _se[pricefuture]))'
 
if `slope_p' < 0.01 		local stars "***" 	    
else if `slope_p' < 0.05 	local stars "**"
else if `slope_p' < 0.10 	local stars "*"
else 					    local stars ""

binscatter price_realization pricefuture, ytitle("price change over the last year (in pp)" " ") xtitle("price expectation a year ago (in pp)")  graphregion(color(white)) bgcolor(white) plotregion(fcolor(white)) text(19 5 "`=ustrunescape("\u03b2")' = `slope'`stars' (`slope_se')", color(black)) text(19.2 1.825 "`=ustrunescape("\u0302")'", color(black)) 

graph export "${path_export_graphs}\figure_3.pdf", replace 

*** Keep only data from 2022 onward
keep if year >= 22 
 
*** Merge with market price data
merge m:1 year month using "${path_data}\marketprices.dta"
keep if _merge != 2
drop _merge 

*** Energy cost share
g share_cost_energy = 0 if energy_prod == 1
replace share_cost_energy = 0.05 if energy_prod == 2
replace share_cost_energy = 0.15 if energy_prod == 3
replace share_cost_energy = 0.25 if energy_prod == 4
replace share_cost_energy = 0.35 if energy_prod == 5
replace share_cost_energy = 0.45 if energy_prod == 6
replace share_cost_energy = 0.55 if energy_prod == 7
replace share_cost_energy = 0.65 if energy_prod == 8
replace share_cost_energy = 0.75 if energy_prod == 9
replace share_cost_energy = 0.85 if energy_prod == 10
replace share_cost_energy = 0.95 if energy_prod == 11
replace share_cost_energy = 1.00 if energy_prod == 12

*** Generate energy input shares
ren (energymix_a energymix_d energymix_e) (share_oil share_coal share_gas)
g share_elec = energymix_f + energymix_g // renewable + conventional electricity
g share_other = energymix_b + energymix_c + energymix_h // other energy sources
g share_major = (share_oil + share_coal + share_gas + share_elec) // oil + coal + gas + electricity
qui sum share_major 
local share_major = `r(mean)' 
qui sum share_oil
local share_oil = `r(mean)' / `share_major'
qui sum share_coal
local share_coal = `r(mean)' / `share_major' 
qui sum share_gas
local share_gas = `r(mean)' / `share_major' 
qui sum share_elec
local share_elec = `r(mean)' / `share_major'
g price_other = (`share_oil' * price_oil + `share_coal' * price_coal + `share_gas' * price_gas + `share_elec' * price_elec) // Assume price for other sources as weighted average of oil, coal, gas, electricity

g price_shock = 100 * share_cost_energy * (share_oil * price_oil + share_coal * price_coal + share_gas * price_gas + share_elec * price_elec) / share_major  
g price_shock_ext = share_cost_energy * (share_oil * price_oil + share_coal * price_coal + share_gas * price_gas + share_elec * price_elec + share_other * price_other)  

save "${path_data}\bopf_shiftshare", replace

log close